<template>
  <div class="app-container">
    <div class="search-container">
      <el-form ref="queryFormRef" :model="queryForm" :inline="true">
        <!-- StartDate  EndDate -->
        <el-form-item prop="keywords">
          <el-input v-model="queryForm.keywords" placeholder="请输入关键字" clearable style="width: 200px" />
          <el-button type="primary" style="margin-left: 0; border-radius: 0" @click="handleQuery"> 搜索 </el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="table-container">
      <el-row :gutter="20">
        <el-col :span="24" :offset="0">
          <el-button v-hasPerm="['projectCorpWageAuth:add']" type="primary" size="default" icon="Plus" @click="Create"
            >新增</el-button
          >
        </el-col>
        <el-col :span="24" :offset="0" style="margin-top: 10px">
          <el-table ref="tableRef" v-loading="loading" :data="tableData" style="width: 100%">
            <el-table-column label="序号" type="index" :index="(index: number) => index + 1" min-width="50" />
            <el-table-column label="项目名称" prop="projectName" min-width="200" align="center" />
            <el-table-column label="参建单位" prop="corpName" min-width="160" align="center" />
            <el-table-column label="代发月份" prop="month" min-width="100" align="center" />
            <el-table-column label="工资录入人数" prop="maxPersonCount" min-width="120" align="center" />
            <el-table-column label="申请发放金额" prop="maxPayAmount" min-width="120" align="center" />
            <el-table-column label="创建时间" prop="createTime" min-width="150" align="center" />

            <el-table-column label="操作" fixed="right" width="120">
              <template #default="scope">
                <el-button
                  v-hasPerm="['projectCorpWageAuth:edit']"
                  type="primary"
                  link
                  size="small"
                  @click="Edit(scope.row)"
                >
                  修改
                </el-button>
                <el-button
                  v-hasPerm="['projectCorpWageAuth:delete']"
                  type="primary"
                  link
                  size="small"
                  @click="Delete(scope.row)"
                >
                  删除
                </el-button>
              </template>
            </el-table-column>
          </el-table>
          <pagination
            v-if="total > 0"
            v-model:total="total"
            v-model:page="queryForm.pageNum"
            v-model:limit="queryForm.pageSize"
            @pagination="handleQuery"
          />
        </el-col>
      </el-row>
    </div>
    <add-form v-if="dialogAddOrEdit.visible" :dialog-add-or-edit="dialogAddOrEdit" @send-close="sendClose" />
  </div>
</template>
<script setup lang="ts">
import addForm from "./addForm.vue";
import { QueryProjectCorpWageAuth, DeleteProjectCorpWageAuth } from "@/api/wage/index";

const loading = ref(false);
const tableData = ref();

const dialogAddOrEdit = reactive<DialogOption>({
  visible: false,
});
//当前选中项
const tableRef = ref();

//新增
const Create = function () {
  dialogAddOrEdit.visible = true;
  dialogAddOrEdit.footBtn = "确认新增";
  dialogAddOrEdit.title = "新增";
};
//编辑
const Edit = function (row: any) {
  dialogAddOrEdit.visible = true;
  dialogAddOrEdit.id = row.id;
  dialogAddOrEdit.footBtn = "确认修改";
  dialogAddOrEdit.title = "修改";
};

//删除
const Delete = function (row: any) {
  ElMessageBox.confirm("确定删除该工资发放授权吗？", "提示", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  })
    .then(() => {
      DeleteProjectCorpWageAuth(row.id).then((res) => {
        if (res.type === 0) {
          if (res.success) {
            ElMessage({
              type: "success",
              message: res.msg,
            });
            handleQuery();
          } else {
            ElMessage({
              type: "error",
              message: res.msg,
            });
          }
        }
      });
    })
    .catch(() => {
      console.log("取消");
    });
};
//关闭弹窗
const sendClose = function (needRefresh: boolean) {
  dialogAddOrEdit.visible = false;
  dialogAddOrEdit.id = undefined;
  if (needRefresh) {
    handleQuery();
  }
};
//查询条件
const queryForm = reactive({
  keywords: "",
  pageNum: 1,
  pageSize: 10,
});
const total = ref(0);

//这是列表数据
const handleQuery = function () {
  loading.value = true;
  QueryProjectCorpWageAuth(queryForm)
    .then((res) => {
      if (res.type === 0) {
        if (res.success) {
          tableData.value = res.data.list;
          queryForm.pageNum = res.data.pageNum;
          queryForm.pageSize = res.data.pageSize;
          total.value = res.data.total;
        } else {
        }
      } else {
      }
    })
    .finally(() => {
      loading.value = false;
    });
};

onMounted(() => {
  handleQuery();
});
</script>
<style scoped>
:deep(.el-button) {
  margin-left: 5px;
}
:deep(.el-input__wrapper) {
  border-radius: 0;
}
</style>
